package com.facedemo.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.facedemo.MyApplication;
import com.facedemo.R;
import com.facedemo.face.FaceConfig;

import java.math.BigDecimal;

public class SettingActivity extends Activity {
    private Button btn_back,btn_cameraId,btn_wh,btn_displayOrientation,btn_dataRotate,
            btn_detectType,btn_live,btn_live_level,btn_iod,btn_dataMirroring;
    private EditText et_w,et_h,et_iod;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
        initData();
    }

    private void initView() {
        btn_cameraId = findViewById(R.id.btn_cameraId);
        btn_wh = findViewById(R.id.btn_wh);
        btn_displayOrientation = findViewById(R.id.btn_displayOrientation);
        btn_dataRotate = findViewById(R.id.btn_dataRotate);
        et_w = findViewById(R.id.et_w);
        et_h = findViewById(R.id.et_h);
        et_iod = findViewById(R.id.et_iod);
        btn_detectType = findViewById(R.id.btn_detectType);
        btn_live = findViewById(R.id.btn_live);
        btn_live_level = findViewById(R.id.btn_live_level);
        btn_iod = findViewById(R.id.btn_iod);
        btn_dataMirroring = findViewById(R.id.btn_dataMirroring);

        btn_cameraId.setOnClickListener(onClickListener);
        btn_wh.setOnClickListener(onClickListener);
        btn_displayOrientation.setOnClickListener(onClickListener);
        btn_dataRotate.setOnClickListener(onClickListener);
        btn_detectType.setOnClickListener(onClickListener);
        btn_live.setOnClickListener(onClickListener);
        btn_live_level.setOnClickListener(onClickListener);
        btn_iod.setOnClickListener(onClickListener);
        btn_dataMirroring.setOnClickListener(onClickListener);

        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {
        btn_cameraId.setText("摄像头ID："+MyApplication.dCamera.cameraId);
        et_w.setText(""+MyApplication.dCamera.cameraWidth);
        et_h.setText(""+MyApplication.dCamera.cameraHeight);
        btn_displayOrientation.setText("摄像头预览旋转："+MyApplication.dCamera.displayOrientation);
        btn_dataRotate.setText("摄像头回调图像旋转："+MyApplication.dCamera.cameraDataRotate);

        if(MyApplication.faceConfig.getFaceDetectType() == FaceConfig.DETECT_TYPE_MAX) {
            btn_detectType.setText("人脸检测：单人脸");
        } else {
            btn_detectType.setText("人脸检测：多人脸");
        }

        if(MyApplication.faceConfig.isUseLiveProcess()) {
            btn_live.setText("人脸检测：使用活体检测");
            btn_live_level.setVisibility(View.VISIBLE);
            if(MyApplication.faceConfig.getLiveLevel() == 1) {
                btn_live_level.setText("活体检测：快速");
            } else if(MyApplication.faceConfig.getLiveLevel() == 2) {
                btn_live_level.setText("活体检测：安全");
            }

        } else {
            btn_live.setText("人脸检测：不使用活体检测");
            btn_live_level.setVisibility(View.GONE);
        }

        et_iod.setText(""+MyApplication.faceConfig.getMinFaceIod());

        btn_dataMirroring.setText("摄像头回调图像镜像："+MyApplication.dCamera.isMirroring);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_cameraId://切换摄像头
                    if (MyApplication.dCamera.cameraId == 1) {
                        MyApplication.dCamera.saveCameraId(0);
                    } else {
                        MyApplication.dCamera.saveCameraId(1);
                    }
                    btn_cameraId.setText("摄像头ID："+MyApplication.dCamera.cameraId);
                    break;
                case R.id.btn_wh:
                    String width = et_w.getText().toString().trim();
                    if (0 == width.length()) {
                        Toast.makeText(SettingActivity.this, "请输入宽度", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    String height = et_h.getText().toString().trim();
                    if (0 == height.length()) {
                        Toast.makeText(SettingActivity.this, "请输入高度", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    int w = Integer.parseInt(width);
                    int h = Integer.parseInt(height);
                    MyApplication.dCamera.saveCameraWidth(w);
                    MyApplication.dCamera.saveCameraHeight(h);
                    Toast.makeText(SettingActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.btn_displayOrientation:
                    if(MyApplication.dCamera.displayOrientation==0) {
                        MyApplication.dCamera.saveDisplayOrientation(90);
                    } else if(MyApplication.dCamera.displayOrientation==90){
                        MyApplication.dCamera.saveDisplayOrientation(180);
                    } else if(MyApplication.dCamera.displayOrientation==180){
                        MyApplication.dCamera.saveDisplayOrientation(270);
                    } else {
                        MyApplication.dCamera.saveDisplayOrientation(0);
                    }
                    btn_displayOrientation.setText("摄像头预览旋转："+MyApplication.dCamera.displayOrientation);
                    break;
                case R.id.btn_dataRotate:
                    if(MyApplication.dCamera.cameraDataRotate==0) {
                        MyApplication.dCamera.saveCameraDataRotate(90);
                    } else if(MyApplication.dCamera.cameraDataRotate==90){
                        MyApplication.dCamera.saveCameraDataRotate(180);
                    } else if(MyApplication.dCamera.cameraDataRotate==180){
                        MyApplication.dCamera.saveCameraDataRotate(270);
                    } else {
                        MyApplication.dCamera.saveCameraDataRotate(0);
                    }
                    btn_dataRotate.setText("摄像头回调图像旋转："+MyApplication.dCamera.cameraDataRotate);
                    break;
                case R.id.btn_dataMirroring:
                    MyApplication.dCamera.saveMirroring(!MyApplication.dCamera.isMirroring);
                    btn_dataMirroring.setText("摄像头回调图像镜像："+MyApplication.dCamera.isMirroring);
                    break;
                case R.id.btn_detectType:
                    if(MyApplication.faceConfig.getFaceDetectType() == FaceConfig.DETECT_TYPE_MAX) {
                        MyApplication.faceConfig.setFaceDetectType(FaceConfig.DETECT_TYPE_MULIT);
                        btn_detectType.setText("人脸检测：多人脸");
                    } else {
                        MyApplication.faceConfig.setFaceDetectType(FaceConfig.DETECT_TYPE_MAX);
                        btn_detectType.setText("人脸检测：单人脸");
                    }
                    break;
                case R.id.btn_live:
                    if(MyApplication.faceConfig.isUseLiveProcess()) {
                        MyApplication.faceConfig.setUseLiveProcess(false);
                        btn_live.setText("人脸检测：不使用活体检测");
                        btn_live_level.setVisibility(View.GONE);
                    } else {
                        MyApplication.faceConfig.setUseLiveProcess(true);
                        btn_live.setText("人脸检测：使用活体检测");

                        btn_live_level.setVisibility(View.VISIBLE);
                        if(MyApplication.faceConfig.getLiveLevel() == 1) {
                            btn_live_level.setText("活体检测：快速");
                        } else if(MyApplication.faceConfig.getLiveLevel() == 2){
                            btn_live_level.setText("活体检测：安全");
                        }
                    }
                    break;
                case R.id.btn_live_level:
                    int curLiveLevel = MyApplication.faceConfig.getLiveLevel();
                    if( curLiveLevel== 1) {
                        MyApplication.faceConfig.setLiveLevel(2);
                        btn_live_level.setText("活体检测：安全");
                    } else {
                        MyApplication.faceConfig.setLiveLevel(1);
                        btn_live_level.setText("活体检测：快速");
                    }
                    break;
                case R.id.btn_iod:
                    String iod = et_iod.getText().toString().trim();
                    if (0 == iod.length()) {
                        Toast.makeText(SettingActivity.this, "请输入内容", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    float minIod = 0;
                    try {
                        minIod = Float.parseFloat(iod);
                        //小数点后两位
                        BigDecimal b = new BigDecimal(minIod);
                        minIod = b.setScale(2,BigDecimal.ROUND_HALF_UP).floatValue();
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                        Toast.makeText(SettingActivity.this, "数据格式错误", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if(minIod<=0 || minIod>=1) {
                        Toast.makeText(SettingActivity.this, "要大于或等于0，小于1", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    MyApplication.faceConfig.setMinFaceIod(minIod);
                    Toast.makeText(SettingActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    };
}
